﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LeSoft.Util;
using LeSoft.Application.Code;
using LeSoft.Application.Bussiness;
using LeSoft.Application.Entity;

namespace LeSoft.Application.Web.Areas.SystemManage.Controllers
{
    public class SettingController : MvcControllerBase
    {
        private SettingBLL settingBLL = new SettingBLL();
        private WorkRoleSettingBLL workroleBLL = new WorkRoleSettingBLL();
        #region 视图
        [HttpGet]
        [HandlerAuthorize(PermissionMode.Enforce)]
        public ActionResult Index()
        {
            return View();
        }

        [HttpGet]
        [HandlerAuthorize(PermissionMode.Enforce)]
        public ActionResult WorkAuditSetting()
        {
            return View();
        }

        [HttpGet]
        [HandlerAuthorize(PermissionMode.Enforce)]
        public ActionResult WorkAuditSettingForm()
        {
            return View();
        }

        [HttpGet]
        [HandlerAuthorize(PermissionMode.Enforce)]
        public ActionResult SettingDetailForm()
        {
            return View();
        }
        #endregion
        #region 获取数据
        /// <summary>
        /// 获取编号
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult GetListHtml()
        {
            string _html = string.Empty;
            var list = EnumHelper.EnumToList<CodeRuleEnum>();
            var list2 = new CodeRuleBLL().GetList();
            var data = from a in list
                       join b in list2 on a.EnumValue.ToString() equals b.EnCode
                       select new
                       {
                           EnCode = b.EnCode,
                           FullName = a.Desction,
                           Mode = new CodeRuleBLL().CheckBillCodeMode(b.EnCode),

                           CodeRuleId = b.RuleId,
                           DemoCode = new CodeRuleBLL().GetDemoBillCode(b.RuleId, b.EnCode).ToString()
                       };
            foreach (var e in data)
            {
                _html += "<tr>";
                _html += " <td style='display:none'>" + e.EnCode + "</td>";
                _html += " <td style='width:50px;'>" + e.FullName + "</td>";
                if (e.Mode) _html += " <td style='width:150px;'><div class='radio'><label><input name=" + e.FullName + " type='radio' checked='checked' value='1' />自动</label><label> <input name=" + e.FullName + " type='radio' value='0' />手动</label></div></td>";
                else _html += " <td style='width:150px;'><div class='radio'><label><input name=" + e.FullName + " type='radio' value='1' />自动</label><label> <input name=" + e.FullName + " type='radio' checked='checked' value='0' />手动</label></div></td>";
                _html += " <td class='formValue'><input id='RuleId' readonly type='text'title='" + e.CodeRuleId + "' value='示例:" + e.DemoCode + "' class='form-control input-profile' /></td>";
                _html += "   <td class='formValue'><a class='btn btn-primary' onclick='SaveContactPanel(" + e.EnCode + ")'><i class='fa fa-save'></i>&nbsp;保&nbsp;存</a></td>";
                _html += "</tr>";
            }
            var JsonData = new
            {
                html = _html
            };
            return Content(JsonData.ToJson());
        }
        [HttpGet]
        public ActionResult GetWorkAuditHtml()
        {
            string _html = string.Empty;
            var list = settingBLL.GetList();
            int index = 0;
            foreach (var item in list)
            {
                _html += "<tr><td class=\"formTitle\" style=\"text-align:center;\">" + item.Name + "</td><td class=\"formValue\"><input type=\"radio\" id=\"Start_" + index + "_Check\" name=\"Check_" + index + "\" checked=\"checked\">启用<input type=\"radio\" id=\"Stop_" + index + "_Check\" name=\"Check_" + index + "\">停用</td><td class=\"formTitle\" style=\"text-align: right;\">审核级数：</td><td class=\"formValue\"><input type=\"number\" class=\"form-control\" readonly=\"readonly\" style=\"border: 0px; border-bottom: 1px solid #ccc; -webkit-box-shadow: inset 0 0px 0px rgba(0,0,0,0); \" value=" + item.AuditLevel + " /></td><td class=\"formTitle\"></td><td class=\"formValue\"><a class=\"btn btn-primary\" onclick=\"btn_WorkAudit('" + item.Id + "')\"><i class=\"fa fa-save\"></i>&nbsp;修&nbsp;改</a></td></tr>";
                index++;
            }
            var JsonData = new
            {
                html = _html
            };
            return Content(JsonData.ToJson());
        }

        [HttpGet]
        public ActionResult GetWorkAuditEntity(string keyValue)
        {
            var data = settingBLL.GetWorkAuditEntity(keyValue);
            return Content(data.ToJson());
        }

        public string GetIsShow(string TypeName, int Level, string Version = "")
        {

            return new WorkSettingBLL().GetIsShowGZF(TypeName, Level, Version);
        }

        /// <summary>
        /// 根据单据Id 查询单据操作记录
        /// </summary>
        /// <param name="BillId"></param>
        /// <returns></returns>
        /// [HttpGet]
        [HandlerAuthorize(PermissionMode.Ignore)]
        [HandlerAction(OperationType.GetList, "单据操作记录", "GetOperateRecordList")]
        public ActionResult GetOperateRecordList(string BillId)
        {
            return Content(new BillOperateRecordBLL().GetList(BillId).ToJson());
        }
        #endregion

        #region 提交数据

        public ActionResult SaveAutoBillCode(string keyValue, string strBillCodeSettingEntity)
        {
            BillCodeSettingEntity billCodeSettingEntity = strBillCodeSettingEntity.ToObject<BillCodeSettingEntity>();
            settingBLL.SaveForm(keyValue, billCodeSettingEntity);
            return Success("操作成功。");
        }

        public ActionResult SaveWorkAuditSetting(string keyValue, WorkAuditSettingEntity workAuditSettingEntity)
        {
            settingBLL.SaveForm(keyValue, workAuditSettingEntity);
            return Success("操作成功。");
        }
        #endregion

        #region 工作角色配置

        public ActionResult GetWorkRoleList()
        {
            var data = workroleBLL.GetList();
            var Html = "";
            var JS = "<script>$(function(){";
            foreach (var item in data)
            {
                Html += "<tr Id='" + item.EnCode + "'>";
                Html += "<td style='display:none' id='Id_" + item.EnCode + "'>" + item.Id + "</td>";
                Html += " <td style='width:100px;'id='Name_" + item.EnCode + "'>" + item.Name + "</td>";
                if (item.RoleType == 1) Html += " <td style='width:150px;'><div class='radio'><label><input name='RoleType_" + item.EnCode + "' type='radio' checked='checked' value='1' onclick='changeType(1," + item.EnCode + ")' />岗位</label><label> <input name='RoleType_" + item.EnCode + "' type='radio' value='2' onclick='changeType(2," + item.EnCode + ")' />角色</label></div></td>";
                if (item.RoleType == 2) Html += " <td style='width:150px;'><div class='radio'><label><input name='RoleType_" + item.EnCode + "' type='radio'  value='1'onclick='changeType(1," + item.EnCode + ")' />岗位</label><label> <input name='RoleType_" + item.EnCode + "' checked='checked' type='radio' value='2' onclick='changeType(2," + item.EnCode + ")' />角色</label></div></td>";
                Html += " <td style='width:210px;'> <div id='RoleId_" + item.EnCode + "' type='select' class='ui-select RoleId' style='width:200px;'></div></td>";
                Html += " <td style='width:70px;margin-left:10px '>应用机构：</td>";
                Html += " <td style='width:210px;'><div id='OrganizeId_" + item.EnCode + "'type='select' class='ui-select ' style='width:200px;' ></div></td>";
                if (item.Mode == 1) Html += " <td style='width:150px;margin-left:10px'><div class='radio'><label><input name='Mode_" + item.EnCode + "' type='radio' checked='checked' value='1' />启用</label><label> <input name='Mode_" + item.EnCode + "' type='radio' value='0' />停用</label></div></td>";
                if (item.Mode == 0) Html += " <td style='width:150px;margin-left:10px'><div class='radio'><label><input name='Mode_" + item.EnCode + "' type='radio'  value='1' />启用</label><label> <input name='Mode_" + item.EnCode + "', checked='checked' type='radio' value='0' />停用</label></div></td>";
                Html += "<td class='formValue'><a class='btn btn-primary' onclick='SaveWorkRole(" + item.EnCode + ")'><i class='fa fa-save'></i>&nbsp;保&nbsp;存</a></td>";
                Html += "</tr>";
                JS += "$('#OrganizeId_" + item.EnCode + "').ComboBoxTree({ url: '../../BaseManage/Organize/GetTreeJson', id: 'value', text: 'text', description: '==请选择==', allowSearch: true  });";
                if (!item.OrganizeId.IsEmpty()) JS += "$('#OrganizeId_" + item.EnCode + "').ComboBoxSetValue(\"" + item.OrganizeId + "\");";
                if (item.RoleType == 1) JS += "$('#RoleId_" + item.EnCode + "').ComboBoxTree({ url: '../../BaseManage/Post/GetTreeJson', id: 'value', text: 'text', description: '==请选择==', allowSearch: true  });";
                if (item.RoleType == 2) JS += "$('#RoleId_" + item.EnCode + "').ComboBoxTree({ url: '../../BaseManage/Role/GetRoleCheckTreeJson', id: 'value', text: 'text', description: '==请选择==', allowSearch: true  });";
                if (!item.RoleId.IsEmpty()) JS += "$('#RoleId_" + item.EnCode + "').ComboBoxSetValue(\"" + item.RoleId + "\");";
            }
            JS += "});</script>";
            var JsonData = new
            {
                Html = Html,
                JS = JS
            };
            return Content(JsonData.ToJson());
        }

        public ActionResult SaveWorkRole(WorkRoleSettingEntity entity)
        {
            try
            {
                workroleBLL.SaveForm(entity.Id, entity);
                return Success("保存成功！");
            }
            catch (Exception e)
            {
                return Error("保存失败：" + e.Message);
                throw;
            }
        }
        #endregion
    }
}
